package com.lagou.edu.course.remote;

import com.lagou.edu.course.api.dto.LessonDTO;
import com.lagou.edu.course.entity.Lesson;
import com.lagou.edu.course.service.ILessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课时service
 *
 * @author lm
 * @since 2021/10/29 17:52
 */
@Slf4j
@Service
public class LessonService {
    @Autowired
    ILessonService iLessonService;

    public List<Lesson> getLessonsBySectionId(Integer sectionId) {
        return iLessonService.lambdaQuery().eq(Lesson::getSectionId, sectionId).list();
    }

    public boolean saveOrUpdate(LessonDTO lessonDTO) {
        Lesson lesson = new Lesson();
        BeanUtils.copyProperties(lessonDTO,lesson);
        if(lesson.getId() == null){
            lesson.setCreateTime(LocalDateTime.now());
        }
        lesson.setUpdateTime(LocalDateTime.now());
        return iLessonService.saveOrUpdate(lesson);
    }

    public LessonDTO getById(Integer lessonId) {
        Lesson lesson = iLessonService.getById(lessonId);
        if(lesson == null){
            return null;
        }
        LessonDTO lessonDTO = new LessonDTO();
        BeanUtils.copyProperties(lesson,lessonDTO);
        return lessonDTO;
    }
}
